/*
 * bsp_pwm_.h
 *
 *  Created on: 2025 Jan 23
 *      Author: SFLY
 */

#ifndef MOTOR_SOURCE_HW_DRIVERS_BSP_PWM_H_
#define MOTOR_SOURCE_HW_DRIVERS_BSP_PWM_H_
#include "driverlib.h"
#include "device.h"
/**
 *  @brief  PWM module
 */
typedef enum {
    PWM_1 = 0,
    PWM_2,
    PWM_3,
    PWM_4,
    PWM_5,
    PWM_6,
    PWM_7,
    PWM_8
} PWM_ModuleEnum;

/**
 *  @brief  PWM channel A or B select type
 */
typedef enum {
  PWM_CHA = 0,                /* PWM Channel A define */
  PWM_CHB = 1                 /* PWM Channel B define */
} PWM_ChannelEnum;

typedef enum {
    EPWM1_A_GPIO = 0,
    EPWM1_B_GPIO,
    EPWM2_A_GPIO,
    EPWM2_B_GPIO,
    EPWM3_A_GPIO,
    EPWM3_B_GPIO,
    EPWM4_A_GPIO,
    EPWM4_B_GPIO,
    EPWM5_A_GPIO,
    EPWM5_B_GPIO,
    EPWM6_A_GPIO,
    EPWM6_B_GPIO,
    EPWM7_A_GPIO,
    EPWM7_B_GPIO,
    EPWM8_A_GPIO,
    EPWM8_B_GPIO
} EPWM_GPIO_Setting;

static __inline void MotorPWM_ForceUVWLow(void) {
    EPwm1Regs.AQCSFRC.bit.CSFA = 0x1;
    EPwm1Regs.AQCSFRC.bit.CSFB = 0x1;
    EPwm2Regs.AQCSFRC.bit.CSFA = 0x1;
    EPwm2Regs.AQCSFRC.bit.CSFB = 0x1;
    EPwm3Regs.AQCSFRC.bit.CSFA = 0x1;
    EPwm3Regs.AQCSFRC.bit.CSFB = 0x1;
}

static __inline void MotorPWM_ForceUVW_HighSideLow(void) {
    EPwm1Regs.AQCSFRC.bit.CSFA = 0x1;
    EPwm2Regs.AQCSFRC.bit.CSFA = 0x1;
    EPwm3Regs.AQCSFRC.bit.CSFA = 0x1;
}

static __inline void MotorPWM_EnableAllPhase(void) {
    EPwm1Regs.AQCSFRC.bit.CSFA = 0x0;
    EPwm1Regs.AQCSFRC.bit.CSFB = 0x0;
    EPwm2Regs.AQCSFRC.bit.CSFA = 0x0;
    EPwm2Regs.AQCSFRC.bit.CSFB = 0x0;
    EPwm3Regs.AQCSFRC.bit.CSFA = 0x0;
    EPwm3Regs.AQCSFRC.bit.CSFB = 0x0;
}

void Bsp_SYNC_Init(void);
void Bsp_EPWM_GPIO_Init(void);
void Bsp_EPWM_Init(void);
void setupPWMs(uint32_t PWMBase, uint16_t PWMPeriod, uint16_t DeadTimeClk);
void setupFaults(uint32_t PWMBase, uint32_t CMPSSBase);
void PWMtriggerADC(uint32_t PWMBase, uint16_t numPWMTicksPerISRTick);
void MotorPWM_SetUVWDuty(uint32_t u32DutyU,uint32_t u32DutyV,uint32_t u32DutyW);
void MotorPWM_SetUVWDuty_Zero(void);
void Init_EnMotorPwm(int bEnable);
uint16_t Motor_GetPWMMaxCount(void);
#endif /* MOTOR_SOURCE_HW_DRIVERS_BSP_PWM_H_ */
